package cc.rengu.igas.channel.upac.biz.impl;

import cc.rengu.igas.channel.upac.common.constant.UpacServiceConstant;
import cc.rengu.igas.channel.upac.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upac.facade.UpacTokenPayService;
import cc.rengu.igas.channel.upac.facade.request.*;
import cc.rengu.igas.channel.upac.facade.response.*;
import cc.rengu.igas.channel.upac.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

public class UpacTokenPayServiceImpl implements UpacTokenPayService {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<UpacSendSmsCodeResponse> upacSendSmsCode(UpacSendSmsCodeRequest upacSendSmsCodeRequest) {
        return null;
    }

    @Override
    public Result<TokenPayOpenQueryResponse> tokenPayOpenQuery(TokenPayOpenQueryRequest tokenPayOpenQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<TokenPayOpenQueryResponse> result = new Result<>();
        TokenPayOpenQueryResponse tokenPayOpenQueryResponse = new TokenPayOpenQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(tokenPayOpenQueryRequest,
                    UpacServiceConstant.TOKEN_PAY_OPEN_QUERY_SERVICE, TokenPayOpenQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((TokenPayOpenQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("无跳转支付开通查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                tokenPayOpenQueryResponse.setRespDesc(((BizException) e).getCode());
                tokenPayOpenQueryResponse.setRespDesc(e.getMessage());
            } else {
                tokenPayOpenQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                tokenPayOpenQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<TokenTransStateQueryRespone> tokenTransStateQuery(TokenTransStateQueryRequest tokenTransStateQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<TokenTransStateQueryRespone> result = new Result<>();
        TokenTransStateQueryRespone tokenTransStateQueryRespone = new TokenTransStateQueryRespone();
        try {
            DubboResult dubboResult = dubboService.callAtomService(tokenTransStateQueryRequest,
                    UpacServiceConstant.TOKEN_TRANSTATE_QUERY_SERVICE, TokenTransStateQueryRespone.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((TokenTransStateQueryRespone) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("无跳转支付状态查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                tokenTransStateQueryRespone.setRespDesc(((BizException) e).getCode());
                tokenTransStateQueryRespone.setRespDesc(e.getMessage());
            } else {
                tokenTransStateQueryRespone.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                tokenTransStateQueryRespone.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<TokenConsumeResponse> tokenConsume(TokenConsumeRequest tokenConsumeRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<TokenConsumeResponse> result = new Result<>();
        TokenConsumeResponse tokenConsumeResponse = new TokenConsumeResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(tokenConsumeRequest,
                    UpacServiceConstant.TOKEN_CONSUME_SERVICE, TokenConsumeResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((TokenConsumeResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("无跳转消费失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                tokenConsumeResponse.setRespDesc(((BizException) e).getCode());
                tokenConsumeResponse.setRespDesc(e.getMessage());
            } else {
                tokenConsumeResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                tokenConsumeResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<TokenConsumeCancelResponse> tokenConsumeCancel(TokenConsumeCancelRequest tokenConsumeCancelRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<TokenConsumeCancelResponse> result = new Result<>();
        TokenConsumeCancelResponse tokenConsumeCancelResponse = new TokenConsumeCancelResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(tokenConsumeCancelRequest,
                    UpacServiceConstant.TOKEN_CONSUME_CANCEL_SERVICE, TokenConsumeCancelResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((TokenConsumeCancelResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("无跳转消费撤销失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                tokenConsumeCancelResponse.setRespDesc(((BizException) e).getCode());
                tokenConsumeCancelResponse.setRespDesc(e.getMessage());
            } else {
                tokenConsumeCancelResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                tokenConsumeCancelResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<TokenConsumeRefundResponse> tokenConsumeRefund(TokenConsumeRefundRequest tokenConsumeRefundRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<TokenConsumeRefundResponse> result = new Result<>();
        TokenConsumeRefundResponse tokenConsumeRefundResponse = new TokenConsumeRefundResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(tokenConsumeRefundRequest,
                    UpacServiceConstant.TOKEN_CONSUME_REFUND_SERVICE, TokenConsumeRefundResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((TokenConsumeRefundResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("无跳转消费退货失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                tokenConsumeRefundResponse.setRespDesc(((BizException) e).getCode());
                tokenConsumeRefundResponse.setRespDesc(e.getMessage());
            } else {
                tokenConsumeRefundResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                tokenConsumeRefundResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<UpacAuthResponse> upacAuth(UpacAuthRequest upacAuthRequest) {
        return null;
    }

    @Override
    public Result<UpacAuthCancelRespone> upacAuthCancel(UpacAuthCancelRequest upacAuthCancelRequest) {
        return null;
    }

    @Override
    public Result<UpacAuthFinishResponse> upacAuthFinish(UpacAuthFinishRequest upacAuthFinishRequest) {
        return null;
    }

    @Override
    public Result<UpacAuthFinishCancelResponse> upacAuthFinishCancel(UpacAuthFinishCancelRequest upacAuthFinishCancelRequest) {
        return null;
    }

    @Override
    public Result<DeleteTokenResponse> deleteToken(DeleteTokenRequest deleteTokenRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<DeleteTokenResponse> result = new Result<>();
        DeleteTokenResponse deleteTokenResponse = new DeleteTokenResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(deleteTokenRequest,
                    UpacServiceConstant.DELETE_TOKEN_SERVICE, DeleteTokenResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((DeleteTokenResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("删除token失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                deleteTokenResponse.setRespDesc(((BizException) e).getCode());
                deleteTokenResponse.setRespDesc(e.getMessage());
            } else {
                deleteTokenResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                deleteTokenResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<UpacUpdateTokenResponse> upacUpdateToken(UpacUpdateTokenRequest upacUpdateTokenRequest) {
        return null;
    }

    @Override
    public Result<TokenPayOpenByOrderIdResponse> tokenApply(TokenPayOpenByOrderIdRequest tokenPayOpenByOrderIdRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<TokenPayOpenByOrderIdResponse> result = new Result<>();
        TokenPayOpenByOrderIdResponse tokenPayOpenByOrderIdResponse = new TokenPayOpenByOrderIdResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(tokenPayOpenByOrderIdRequest,
                    UpacServiceConstant.TOKEN_APPLY_SERVICE, TokenPayOpenByOrderIdResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((TokenPayOpenByOrderIdResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("查询token信息失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                tokenPayOpenByOrderIdResponse.setRespDesc(((BizException) e).getCode());
                tokenPayOpenByOrderIdResponse.setRespDesc(e.getMessage());
            } else {
                tokenPayOpenByOrderIdResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                tokenPayOpenByOrderIdResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }
}
